import time
import gevent

from gevent import monkey
#标准库的IO是阻塞的
#改成非阻塞IO
monkey.patch_all()
#保证这三个在requests 上面

import requests

headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"

}
def fn(url,i):
    print("协程%d开始" % i)
    response = requests.get(url,headers=headers)
    html = response.text
    print("协程%d结束，%d" %(i,len(html)))
if __name__ == '__main__':
    urls = [
        "https://www.baidu.com",
        "https://www.so.com",
        "https://www.sogou.com",
        "http://www.qfedu.com",
        "http://www.ifeng.com",

    ]
    #开始时间
    start = time.time()
    g_list = [] #存放协程
    for i in range(len(urls)):
        #同步
        # fn(urls[i],i)
        #多协程 异步来实现
        g = gevent.spawn(fn,urls[i],i)
        g_list.append(g)
    #启动协程
    gevent.joinall(g_list)
    end = time.time()

    print(end-start)